package com.buaa.util;

import java.util.Set;

public class StringUtils {

	/**
	 * （可见字符）全角 转 半角
	 * 
	 * @param str
	 * @return
	 */
	private static char sbcToDbc(char ch) {
		if (ch == 12288)
			return ' ';
		// if (ch == '（' || ch == '）')
		// return ch;
		if (65281 <= ch && ch <= 65374)
			return (char) (ch - 65248);
		return ch;
	}

	/**
	 * 判断字符串是否为中英文混合
	 * 
	 * @param str
	 * @param symbol
	 * @return
	 */
	public static boolean isZHEN(String str, Set<Character> symbol) {
		char ch;
		for (int i = 0; i < str.length(); i++) {
			ch = str.charAt(i);
			ch = sbcToDbc(ch);
			if (!isChinese(ch) && !isLetter(ch))
				if (symbol == null || symbol != null && !symbol.contains(ch)) {
//					System.out.println("\"" + ch + "\"");
					return false;
				}
		}
		return true;
	}

	/**
	 * 过滤出中英文字符串
	 * 
	 * @param str
	 * @param symbol
	 * @return
	 */
	public static String filterZHEN(String str, Set<Character> symbol) {
		char ch;
		StringBuilder sb = new StringBuilder();
		int start = 0, end = 0;
		for (; end < str.length(); end++) {
			ch = str.charAt(end);
			ch = sbcToDbc(ch);
			if (!isChinese(ch) && !isLetter(ch))
				if (symbol == null || symbol != null && !symbol.contains(ch)) {
					if (start != end)
						sb.append(str.substring(start, end));
					start = end + 1;
				}
		}
		if (start != end)
			sb.append(str.substring(start, end));
		return sb.toString();
	}

	/**
	 * 判断字符串是否为全中文
	 * 
	 * @param str
	 * @param symbol
	 * @return
	 */
	public static boolean isZH(String str, Set<Character> symbol) {
		char ch;
		for (int i = 0; i < str.length(); i++) {
			ch = str.charAt(i);
			ch = sbcToDbc(ch);
			if (!isChinese(ch))
				if (symbol == null || symbol != null && !symbol.contains(ch))
					return false;
		}
		return true;
	}

	/**
	 * 过滤出中文字符串
	 * 
	 * @param str
	 * @param symbol
	 * @return
	 */
	public static String filterZH(String str, Set<Character> symbol) {
		char ch;
		StringBuilder sb = new StringBuilder();
		int start = 0, end = 0;
		for (; end < str.length(); end++) {
			ch = str.charAt(end);
			ch = sbcToDbc(ch);
			if (!isChinese(ch))
				if (symbol == null || symbol != null && !symbol.contains(ch)) {
					if (start != end)
						sb.append(str.substring(start, end));
					start = end + 1;
				}
		}
		if (start != end)
			sb.append(str.substring(start, end));
		return sb.toString();

	}

	/**
	 * 判断字符是否为中文
	 * 
	 * @param c
	 * @return
	 */
	private static boolean isChinese(char c) {
		// 中文字符 \u4E00-\u9FA5，只是部分
		// 详情：http://blog.csdn.net/xyls12345/article/details/23942533
		// 0x3007 === 〇
		// 汉字与十六进制转换：http://utf8.supfree.net/
		return c >= 0x4E00 && c <= 0x9FA5 || c >= 0x3007 && c <= 0x3007;
	}

	/**
	 * 判断字符串是否为全英文
	 * 
	 * @param str
	 * @param symbol
	 * @return
	 */
	public static boolean isEN(String str, Set<Character> symbol) {
		char ch;
		for (int i = 0; i < str.length(); i++) {
			ch = str.charAt(i);
			ch = sbcToDbc(ch);
			if (!isLetter(ch))
				if (symbol == null || symbol != null && !symbol.contains(ch))
					return false;
		}
		return true;
	}

	/**
	 * 过滤出英文字符串
	 * 
	 * @param str
	 * @param symbol
	 * @return
	 */
	public static String filterEN(String str, Set<Character> symbol) {
		char ch;
		StringBuilder sb = new StringBuilder();
		int start = 0, end = 0;
		for (int i = 0; i < str.length(); i++) {
			ch = str.charAt(i);
			ch = sbcToDbc(ch);
			if (!isLetter(ch))
				if (symbol == null || symbol != null && !symbol.contains(ch)) {
					if (start != end)
						sb.append(str.substring(start, end));
					start = end + 1;
				}
		}
		if (start != end)
			sb.append(str.substring(start, end));
		return sb.toString();
	}

	/**
	 * 判断字符是否为字母
	 * 
	 * @param c
	 * @return
	 */
	private static boolean isLetter(char c) {
		return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
	}
}
